package com.wanjia.estate.service.redis;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.wanjia.estate.pojo.document.InvoiceInfo;
import com.wanjia.estate.service.MoneyCacheService;
import com.wanjia.estate.template.IntegerRedisTemplate;
import com.wanjia.estate.template.InvoiceInfoRedisTemplate;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

@Service
public class RedisMoneyCacheServiceImpl implements MoneyCacheService {
    public static final String INFOS = "{}:infos";
    public static final String INDEX = "{}:index";


    @Setter(onMethod_ = {@Autowired})
    private InvoiceInfoRedisTemplate invoiceInfoRedisTemplate;


    @Setter(onMethod_ = {@Autowired})
    private IntegerRedisTemplate integerRedisTemplate;

    private String getInfosKey(String slbh) {
        return StrUtil.format(INFOS, slbh);
    }

    private String getIndexKey(String slbh) {
        return StrUtil.format(INDEX, slbh);
    }

    @Override
    public List<InvoiceInfo> getInfos(String slbh) {
        ListOperations<String, InvoiceInfo> ops = invoiceInfoRedisTemplate.opsForList();
        String infosKey = getInfosKey(slbh);
        return ops.range(infosKey, 0, - 1);
    }

    @Override
    public Integer getIndex(String slbh) {
        String indexKey = getIndexKey(slbh);
        return integerRedisTemplate.opsForValue().get(indexKey);
    }

    @Override
    public void set(String slbh, Integer index, List<InvoiceInfo> infos) {
        String infosKey = getInfosKey(slbh);
        String indexKey = getIndexKey(slbh);
        invoiceInfoRedisTemplate.delete(infosKey);
        if (CollUtil.isNotEmpty(infos)) {
            ListOperations<String, InvoiceInfo> ops = invoiceInfoRedisTemplate.opsForList();
            infos.forEach(info -> ops.rightPush(infosKey, info));
        }
        integerRedisTemplate.opsForValue().set(indexKey, index);
        invoiceInfoRedisTemplate.expire(infosKey, Duration.ofDays(30));
        integerRedisTemplate.expire(indexKey, Duration.ofDays(30));
    }

    @Override
    public void setIndex(String slbh, Integer index) {
        String indexKey = getIndexKey(slbh);
        integerRedisTemplate.opsForValue().set(indexKey, index);
        integerRedisTemplate.expire(indexKey, Duration.ofDays(30));
    }

    @Override
    public void appendInvoiceInfo(String slbh, InvoiceInfo info) {
        String infosKey = getInfosKey(slbh);
        if (info != null) {
            ListOperations<String, InvoiceInfo> ops = invoiceInfoRedisTemplate.opsForList();
            ops.rightPush(infosKey, info);
        }
        invoiceInfoRedisTemplate.expire(infosKey, Duration.ofDays(30));
    }

    @Override
    public void appendInvoiceInfos(String slbh, List<InvoiceInfo> infos) {
        String infosKey = getInfosKey(slbh);
        if (CollUtil.isNotEmpty(infos)) {
            ListOperations<String, InvoiceInfo> ops = invoiceInfoRedisTemplate.opsForList();
            infos.forEach(info -> ops.rightPush(infosKey, info));
        }
        invoiceInfoRedisTemplate.expire(infosKey, Duration.ofDays(30));
    }

    @Override
    public void clear(String slbh) {
        String infosKey = getInfosKey(slbh);
        String indexKey = getIndexKey(slbh);
        invoiceInfoRedisTemplate.delete(infosKey);
        integerRedisTemplate.delete(indexKey);
    }
}
